package com.likou;

/**
 * Author: 陈桂明
 * Date: 2025/9/9 9:21
 * Description: q543
 */
public class q543 {

    int ans = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        depth(root);

        return ans - 1;
    }

    private int depth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int left = depth(root.left);
        int right = depth(root.right);
        ans = Math.max(ans, (left + right) + 1);

        return Math.max(left, right) + 1;
    }
}
